#include "v_additem.h"
#include "ui_v_additem.h"

V_AddItem::V_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::V_AddItem)
{
    ui->setupUi(this);
}

V_AddItem::~V_AddItem()
{
    delete ui;
}

void V_AddItem::on_btn_pick_clicked()
{
    QString path=QFileDialog::getOpenFileName(this,"选择一个文件");
    if(path.isEmpty())
        return;

    QString baseName=QFileInfo(path).baseName();
    ui->entry->setText(baseName);
    ui->sha1->setText("SHA1计算中……");

    QString sha1=sp_verifyFile(QCryptographicHash::Sha1,path);
    ui->sha1->setText(sha1);
}

void V_AddItem::on_btn_submit_clicked()
{
    QString versionStr=ui->version->text().trimmed();
    int version=versionStr.toInt();
    if(version == 0)
    {
        MESSAGE_INPUT_ERROR
    }

    QString entry=ui->entry->text().trimmed();
    QString sha1=ui->sha1->text().trimmed();
    if(entry.isEmpty() || sha1.isEmpty())
    {
        MESSAGE_DETAIL_EMPTY
    }

    QString location=entry + ".zip";

    QJsonObject obj;
    obj.insert("entry",entry);
    obj.insert("version",version);
    obj.insert("location",location);
    obj.insert("sha1",sha1);
    emit addItem(obj);
    accept();
}

void V_AddItem::on_btn_paste_clicked()
{
    QString context=qApp->clipboard()->text();
    if(!context.isEmpty())
        ui->sha1->setText(context);
}
